package com.hcrazy.manchester.app.jdbc;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import com.hcrazy.manchester.app.pojo.PageModel;

public abstract class BaseDAO<T> extends NamedParameterJdbcDaoSupport {

	protected final static String PAGE_SQL_PREFIX = "select m.* from (";
	protected final static String PAGE_SQL_END = ") m limit ?,?";

	protected int commonUpdate(String sql, Object... params) throws Exception {
		try {
			return this.getJdbcTemplate().update(sql, params);
		} catch (DataAccessException e) {
			throw new Exception(e.getMessage());
		}
	}

	protected int update(String namedSql, Object javaBean) throws Exception {
		try {
			SqlParameterSource paramSource = new BeanPropertySqlParameterSource(javaBean);
			return this.getNamedParameterJdbcTemplate().update(namedSql, paramSource);
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}

	protected T getJavaBean(String sql, Class<T> returnType, Object... paramValue) throws Exception {
		RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(returnType);
		try {
			return this.getJdbcTemplate().queryForObject(sql, rowMapper, paramValue);
		} catch (Exception ex) {
			if(ex.getMessage().contains("expected 1, actual 0")){
				return null;
			}
			throw new Exception(ex.getMessage());
		}
	}

	protected List<T> getList(String sql, Class<T> returnType, Object... paramValue) throws Exception {
		try {
			RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(returnType);
			return this.getJdbcTemplate().query(sql, rowMapper, paramValue);
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}

	protected List<T> getList(String sql, Class<T> returnType) {
		RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(returnType);
		return this.getJdbcTemplate().query(sql, rowMapper);
	}

	@Deprecated
	protected int getCount(String countSQL, List<Object> paramValue) {
		return this.getJdbcTemplate().queryForInt(countSQL, paramValue.toArray());
	}

	@Deprecated
	protected int getCount(String countSQL, Object... paramValue) {
		return this.getJdbcTemplate().queryForInt(countSQL, paramValue);
	}
	
	@Deprecated
	protected int getCount(String countSQL) {
		return this.getJdbcTemplate().queryForInt(countSQL);
	}

	protected PageModel getPageModel(PageModel model, StringBuilder querySQL, StringBuilder countSQL, StringBuilder whereSQL, List<Object> paramList, Class<T> returnType)
			throws Exception {
		querySQL.append(whereSQL);
		countSQL.append(whereSQL);
		// 计算总记录数
		int allCount = this.getCount(countSQL.toString(), paramList);
		// 获取分页记录集
		// 1。构造完整的分页语句
		querySQL.insert(0, PAGE_SQL_PREFIX);
		querySQL.append(PAGE_SQL_END);
		// 2.把分页语句中的参数值加入到paramList中
		paramList.add((model.getPageNum() - 1) * model.getNumPerPage());
		paramList.add(model.getNumPerPage());
		List<T> result = this.getList(querySQL.toString(), returnType, paramList.toArray());
		PageModel models = new PageModel();
		models.setTotalCount(allCount);
		models.setNumPerPage(model.getNumPerPage());
		models.setPageNum(model.getPageNum());
		models.setResult(result);

		return models;
	}

	public int getIncreamentId() throws Exception {
		String sql = "select LAST_INSERT_ID()";
		return this.getJdbcTemplate().queryForInt(sql);
	}
	
}
